import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';
import { UpdateSubmissionRequest } from '@/types/submission';

async function handlePost(request: NextRequest): Promise<{ success: boolean; message: string }> {
  const authResult = await authenticateRequest(request);
  if (!authResult.success || !authResult.user || authResult.user.role !== 1) {
    throw new Error('权限不足');
  }

  const body: UpdateSubmissionRequest = await request.json();
  const { id, status, remark } = body;

  if (!id || status === undefined || status === null) {
    throw new Error('参数不完整');
  }

  await prisma.submission.update({
    where: { id },
    data: {
      status,
      remark: remark || null,
      updatedAt: new Date(),
    },
  });

  return {
    success: true,
    message: '更新成功',
  };
}

export const POST = withMiddleware(handlePost);